/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.grnt.common;

import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Set;
import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

/**
 *
 * @author sayid
 */
public class ContextWraper{

	private static ServletContext entity;

	/**
	 * A exception which is throwed by class ContextWraper.
	 */
	public static class ContextRewrapException extends Exception{
		/**
		 *
		 * @param msg
		 */
		public ContextRewrapException(String msg) {
			super(msg);
		}
	}

	/**
	 *
	 * Wrapers the input context.
	 * @param context The parameter is the context will be wrapped.
	 * @throws org.grnt.common.ContextWraper.ContextRewrapException
	 */
	public static void wrap(ServletContext context) throws ContextRewrapException{
		if (entity == null) {
			entity = context;
		} else {
			throw new ContextRewrapException("ContextWrapper can't rewrap.");
		}
	}

	/**
	 *
	 * Represents wether it is actived. The ContextWraper is actived means entity
	 * isn't null.
	 * @return
	 */
	public final static boolean isActive() {
		return entity != null;
	}

	/**
	 *
	 * @return The wrapped context.
	 */
	public final static ServletContext getEntity() {
		return entity;
	}

	/**
	 *
	 * @see ServletContext#getContext(java.lang.String)
	 * @param uripath
	 * @return
	 */
	public final static ServletContext getContext(String uripath) {
		return entity.getContext(uripath);
	}

	/**
	 *
	 * @see ServletContext#getContextPath()
	 * @return
	 */
	public final static String getContextPath() {
		return entity.getContextPath();
	}

	/**
	 * @see ServletContext#getMajorVersion()
	 * @return
	 */
	public final static int getMajorVersion() {
		return entity.getMajorVersion();
	}

	/**
	 *
	 * @see ServletContext#getMinorVersion()
	 * @return
	 */
	public final static int getMinorVersion() {
		return entity.getMinorVersion();
	}

	/**
	 *
	 * @see ServletContext#getMimeType(String file)
	 * @param file
	 * @return
	 */
	public final static String getMimeType(String file) {
		return entity.getMimeType(file);
	}

	/**
	 *
	 * @see ServletContext#getResourcePaths(String path)
	 * @param path
	 * @return
	 */
	public final static Set getResourcePaths(String path) {
		return entity.getResourcePaths(path);
	}

	/**
	 *
	 * @see ServletContext#getResource(String path)
	 * @param path
	 * @return
	 * @throws MalformedURLException
	 */
	public final static URL getResource(String path) throws MalformedURLException {
		return entity.getResource(path);
	}

	/**
	 *
	 * @see ServletContext#getResourceAsStream(String path)
	 * @param path
	 * @return
	 */
	public final static InputStream getResourceAsStream(String path) {
		return entity.getResourceAsStream(path);
	}

	/**
	 *
	 * @see ServletContext#getRequestDispatcher(String path)
	 * @param path
	 * @return
	 */
	public final static RequestDispatcher getRequestDispatcher(String path) {
		return entity.getRequestDispatcher(path);
	}

	/**
	 *
	 * @see ServletContext#getNameDispatcher(String name)
	 * @param name
	 * @return
	 */
	public final static RequestDispatcher getNamedDispatcher(String name) {
		return entity.getNamedDispatcher(name);
	}

	/**
	 *
	 * @see ServletContext#getServlet(String name)
	 * @param name
	 * @return
	 * @throws ServletException
	 */
	public final static Servlet getServlet(String name) throws ServletException {
		return entity.getServlet(name);
	}

	/**
	 *
	 * @see ServletContext#getServlets()
	 * @return
	 */
	public final static Enumeration getServlets() {
		return entity.getServlets();
	}

	/**
	 *
	 * @see ServletContext#getServletNames()
	 * @return
	 */
	public final static Enumeration getServletNames() {
		return entity.getServletNames();
	}

	/**
	 *
	 * @see ServletContext#log(String msg)
	 * @param msg
	 */
	public final static void log(String msg) {
		entity.log(msg);
	}

	/**
	 *
	 * @see ServletContext#log(Exception exception, String msg)
	 * @param exception
	 * @param msg
	 */
	public final static void log(Exception exception, String msg) {
		entity.log(exception, msg);
	}

	/**
	 *
	 * @see ServletContext#log(String message, Throwable throwable)
	 * @param message
	 * @param throwable
	 */
	public final static void log(String message, Throwable throwable) {
		entity.log(message, throwable);
	}

	/**
	 *
	 * @see ServletContext#getRealPath(String path)
	 * @param path
	 * @return
	 */
	public final static String getRealPath(String path) {
		return entity.getRealPath(path);
	}

	/**
	 *
	 * @see ServletContext#getServerInfo()
	 * @return
	 */
	public final static String getServerInfo() {
		return entity.getServerInfo();
	}

	/**
	 *
	 * @see ServletContext#getInitParameter(String name)
	 * @param name
	 * @return
	 */
	public final static String getInitParameter(String name) {
		return entity.getInitParameter(name);
	}

	/**
	 *
	 * @see ServletContext#getInitParameterNames()
	 * @return
	 */
	public final static Enumeration getInitParameterNames() {
		return entity.getInitParameterNames();
	}

	/**
	 *
	 * @see ServletContext#getAttribute(String name)
	 * @param name
	 * @return
	 */
	public final static Object getAttribute(String name) {
		return entity.getAttribute(name);
	}

	/**
	 * @see ServletContext#getAttributeNames()
	 * @return
	 */
	public final static Enumeration getAttributeNames() {
		return entity.getAttributeNames();
	}

	/**
	 *
	 * @see ServletContext#setAttribute(java.lang.String, java.lang.Object)
	 * @param name
	 * @param object
	 */
	public final static void setAttribute(String name, Object object) {
		entity.setAttribute(name, object);
	}

	/**
	 *
	 * @see ServletContext#removeAttribute(java.lang.String)
	 * @param name
	 */
	public final static void removeAttribute(String name) {
		entity.removeAttribute(name);
	}

	/**
	 *
	 * @see ServletContext#getServletContextName()
	 * @return
	 */
	public final static String getServletContextName() {
		return entity.getServletContextName();
	}
}
